package ServletMerchandise;

import Dao.McdDao;
import Dao.MctDao;
import Entity.Merchandise;
import Util.Page;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet(name = "Servlet.ServletMcdUpdate", value = "/mcdupdate")
public class ServletMcdUpdate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");

        //列表刷新
        Merchandise merchandise = null;
        try {
            merchandise = McdDao.queryByid(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
//        System.out.println(merchandise);

        ArrayList<Merchandise> list = null;
        try {
            list = McdDao.queryBymctid(merchandise.getMctid());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String username = null;
        try {
            username = McdDao.queryusername(list.get(0).getMctid());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        request.setAttribute("merchandise", merchandise);
        request.setAttribute("list",list);
        request.setAttribute("username",username);

        //获取页码
        int pagenum = 1,total = list.size();
        pagenum = Integer.parseInt(request.getParameter("pagenum"));
        Page page = new Page(pagenum,total);
        request.setAttribute("page",page);
//        System.out.println("get:"+page);

        request.getRequestDispatcher("mcdupdate.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        //列表刷新
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        double price = Double.parseDouble(request.getParameter("price"));
        int num = Integer.parseInt(request.getParameter("num"));
        int mctid = Integer.parseInt(request.getParameter("mctid"));
        String url = request.getParameter("url");
//        System.out.println(url);
        Merchandise merchandise = new Merchandise(id, name, price, num, mctid, url);
//        System.out.println(merchandise);
        try {
            McdDao.update(merchandise);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        request.setAttribute("merchandise", merchandise);

        ArrayList<Merchandise> list = null;
        try {
            list = McdDao.queryBymctid(mctid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        request.setAttribute("list", list);
        String username = request.getParameter("username");
        request.setAttribute("username",username);

        //获取页码
        int pagenum = 1,total = list.size();
        pagenum = Integer.parseInt(request.getParameter("pagenum"));
        Page page = new Page(pagenum,total);
        request.setAttribute("page",page);
//        System.out.println("post:"+page);

        request.getRequestDispatcher("mcdupdate.jsp").forward(request, response);
    }
}
